package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.annotation.ElementType;
import java.util.ArrayList;

public class LogJpanel extends JPanel implements ActionListener {
    private ATMJframe atmJframe;
    private JButton btnLog;
    private Icon background = Util.getimagesIcon("/images/buttonLand.jpg");
    private int count =3; //剩余登录机会
    private JTextField textFieldId;
    private JPasswordField textFieldPassWord;

    public LogJpanel(ATMJframe atmJframe) {
        this.atmJframe = atmJframe;
        this.setLayout(null);
        this.setOpaque(false);
        btnLog = new JButton("", background);
        btnLog.setBounds(420, 400, 140, 50);
        this.add(btnLog);

        JLabel Id = new JLabel("账号：");
        Id.setBounds(300, 200, 100, 50);
        Id.setFont(new Font("宋体", Font.PLAIN, 18));
        this.add(Id);

        textFieldId = new JTextField();
        textFieldId.setBounds(350, 200, 300, 50);
        this.add(textFieldId);

        JLabel passWord = new JLabel("密码：");
        passWord.setBounds(300, 270, 100, 50);
        passWord.setFont(new Font("宋体", Font.PLAIN, 18));
        this.add(passWord);

        textFieldPassWord = new JPasswordField();
        textFieldPassWord.setBounds(350, 270, 300, 50);
        this.add(textFieldPassWord);


        btnLog.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {


        for (int i = 0; i < atmJframe.getUsersArrayList().size(); i++) {
            if (atmJframe.getUsersArrayList().get(i).getId().equals(textFieldId.getText())
                    && atmJframe.getUsersArrayList().get(i).getPassWord().equals(String.valueOf(textFieldPassWord.getPassword()))) {
                atmJframe.switchPanel("menu");
                atmJframe.setUser(atmJframe.getUsersArrayList().get(i));
                count=3;

                return;
            }
        }
        count--;
        if (count==0){
            JOptionPane.showMessageDialog(this,"机会用完，卡被吞掉");
            System.exit(0);
        }
        JOptionPane.showMessageDialog(this,"登录失败还剩"+count+"次机会");

    }
}//last
